<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="78px">
      <el-form-item label="企业名称" prop="disposalLandId">
        <el-select v-model="queryParams.disposalLandId" placeholder="请选择企业" clearable filterable="">
          <el-option v-for="dict in disposalLandOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择所属项目" clearable filterable
          :popper-append-to-body="false" class="product-style">
          <el-option v-for="dict in projectOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="所属项目" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入所属项目" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="过磅类型" prop="weighingType">
        <el-select v-model="queryParams.weighingType" placeholder="请选择过磅类型" clearable>
          <el-option v-for="dict in dict.type.weighing_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="回收类型" prop="garbageType">
        <el-select v-model="queryParams.garbageType" placeholder="请选择回收垃圾类型" clearable>
          <el-option v-for="dict in dict.type.garbage_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="carNumber">
        <el-input v-model="queryParams.carNumber" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="流水号" prop="serialNumber">
        <el-input v-model="queryParams.serialNumber" placeholder="请输入流水号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发货单位" prop="shipper">
        <el-input v-model="queryParams.shipper" placeholder="请输入发货单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货单位" prop="receivingUnit">
        <el-input v-model="queryParams.receivingUnit" placeholder="请输入收货单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="货名" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入货名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateData" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['disposal:approach:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['disposal:approach:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['disposal:approach:remove']">批量删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['disposal:approach:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="approachList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="资源利用化企业" align="center" prop="disposalLandName" width="200" show-overflow-tooltip />
      <el-table-column label="所属项目" align="center" prop="projectName" width="200" show-overflow-tooltip />
      <el-table-column label="过磅类型" align="center" prop="weighingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.weighing_type" :value="scope.row.weighingType" />
        </template>
      </el-table-column>
      <el-table-column label="流水号" align="center" prop="serialNumber" width="100" show-overflow-tooltip />
      <el-table-column label="车牌号" align="center" prop="carNumber" width="100" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createData" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createData) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="垃圾类型" align="center" prop="garbageType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.garbage_type" :value="scope.row.garbageType" />
        </template>
      </el-table-column>
      <el-table-column label="实重" align="center" prop="garbageWeight" />
      <el-table-column label="净重" align="center" prop="netWeight" />
      <el-table-column label="净重图片" align="center" prop="tareImg">
        <template slot-scope="scope" v-if="scope.row.tareImg != null">
          <image-preview :src="scope.row.tareImg" :width=50 :height=50 />
        </template>
      </el-table-column>
      <el-table-column label="皮重" align="center" prop="tare" />
      <el-table-column label="皮重时间" align="center" prop="tareTime" width="100">
      </el-table-column>
      <el-table-column label="皮重司磅员" align="center" prop="tareWeigher" />
      <el-table-column label="皮重磅号" align="center" prop="tareHeavy" />
      <el-table-column label="毛重" align="center" prop="grossWeight" />
      <el-table-column label="毛重时间" align="center" prop="grossWeightTime" width="100">
      </el-table-column>
      <el-table-column label="毛重司磅员" align="center" prop="grossWeightman" />
      <el-table-column label="毛重磅号" align="center" prop="grossHeavyweight" />
      <el-table-column label="毛重图片" align="center" prop="grossImg">
        <template slot-scope="scope" v-if="scope.row.grossImg != null">
          <image-preview :src="scope.row.grossImg" :width=50 :height=50 />
        </template>
      </el-table-column>
      <el-table-column label="扣重" align="center" prop="deduction" />
      <el-table-column label="方量" align="center" prop="squareAmount" />
      <el-table-column label="折方系数" align="center" prop="factor" />
      <el-table-column label="一次过磅重" align="center" prop="oneWeighing" />
      <el-table-column label="一次过磅时间" align="center" prop="weighingTime" width="100">
      </el-table-column>
      <el-table-column label="二次过磅重" align="center" prop="secondWeighing" />
      <el-table-column label="二次过磅时间" align="center" prop="secondWeighingTime" width="100">
      </el-table-column>
      <el-table-column label="货名" align="center" prop="goodsName" />
      <el-table-column label="发货单位" align="center" prop="shipper" width="100" show-overflow-tooltip />
      <el-table-column label="收货单位" align="center" prop="receivingUnit" width="100" show-overflow-tooltip />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="过磅费" align="center" prop="weighingFee" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['disposal:approach:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['disposal:approach:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改处置终端车辆进场明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :inline="true" label-position="left" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="资源利用化企业" prop="disposalLandId">
          <el-select v-model="form.disposalLandId" placeholder="请选择处置地" clearable filterable="" style="width:92%">
            <el-option v-for="dict in disposalLandOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="过磅类型" prop="weighingType">
          <el-select v-model="form.weighingType" placeholder="请选择过磅类型" style="width:92%">
            <el-option v-for="dict in dict.type.weighing_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回收垃圾类型" prop="garbageType">
          <el-select v-model="form.garbageType" placeholder="请选择回收垃圾类型">
            <el-option v-for="dict in dict.type.garbage_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流水号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入流水号" maxlength="20" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createData">
          <el-date-picker clearable v-model="form.createData" type="date" value-format="yyyy-MM-dd" style="width:88%"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实重" prop="garbageWeight">
          <el-input-number v-model="form.garbageWeight" placeholder="请输入垃圾重量" />
        </el-form-item>
        <el-form-item label="净重" prop="netWeight">
          <el-input-number v-model="form.netWeight" placeholder="请输入净重" />
        </el-form-item>
        <el-form-item label="皮重" prop="tare">
          <el-input-number v-model="form.tare" placeholder="请输入皮重" />
        </el-form-item>
        <el-form-item label="皮重时间" prop="tareTime">
          <el-date-picker clearable v-model="form.tareTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择皮重时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="皮重司磅员" prop="tareWeigher">
          <el-input v-model="form.tareWeigher" placeholder="请输入皮重司磅员" max="20" />
        </el-form-item>
        <el-form-item label="皮重磅号" prop="tareHeavy">
          <el-input v-model="form.tareHeavy" placeholder="请输入皮重磅号" max="20" />
        </el-form-item>
        <el-form-item label="毛重" prop="grossWeight">
          <el-input-number v-model="form.grossWeight" placeholder="请输入毛重" />
        </el-form-item>
        <el-form-item label="毛重时间" prop="grossWeightTime">
          <el-date-picker clearable v-model="form.grossWeightTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择毛重时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="毛重司磅员" prop="grossWeightman">
          <el-input v-model="form.grossWeightman" placeholder="请输入毛重司磅员" max="20" />
        </el-form-item>
        <el-form-item label="毛重磅号" prop="grossHeavyweight">
          <el-input v-model="form.grossHeavyweight" placeholder="请输入毛重磅号" max="20" />
        </el-form-item>
        <el-form-item label="扣重" prop="deduction">
          <el-input-number v-model="form.deduction" placeholder="请输入扣重" />
        </el-form-item>
        <el-form-item label="方量" prop="squareAmount">
          <el-input-number v-model="form.squareAmount" placeholder="请输入方量" />
        </el-form-item>
        <el-form-item label="折方系数" prop="factor">
          <el-input-number v-model="form.factor" placeholder="请输入折方系数" />
        </el-form-item>
        <el-form-item label="一次过磅重" prop="oneWeighing">
          <el-input v-model="form.oneWeighing" placeholder="请输入一次过磅重" />
        </el-form-item>
        <el-form-item label="一次过磅时间" prop="weighingTime">
          <el-date-picker clearable v-model="form.weighingTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择一次过磅时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="二次过磅重" prop="secondWeighing">
          <el-input-number v-model="form.secondWeighing" placeholder="请输入二次过磅重" />
        </el-form-item>
        <el-form-item label="二次过磅时间" prop="secondWeighingTime">
          <el-date-picker clearable v-model="form.secondWeighingTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择二次过磅时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="货名" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入货名" maxlength="20" />
        </el-form-item>
        <el-form-item label="发货单位" prop="shipper">
          <el-input v-model="form.shipper" placeholder="请输入发货单位" maxlength="20" />
        </el-form-item>
        <el-form-item label="收货单位" prop="receivingUnit">
          <el-input v-model="form.receivingUnit" placeholder="请输入收货单位" maxlength="20" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" maxlength="20" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input-number v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input-number v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="过磅费" prop="weighingFee">
          <el-input-number v-model="form.weighingFee" placeholder="请输入过磅费" />
        </el-form-item>
        <el-form-item label="净重图片">
          <image-upload v-model="form.tareImg" :limit=3 />
        </el-form-item>
        <el-form-item label="毛重图片" prop="grossImg">
          <image-upload v-model="form.grossImg" :limit=2 />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApproach, getApproach, delApproach, addApproach, updateApproach } from "@/api/disposal/approach";
import { allDisposalLand } from "@/api/disposal/disposalLand";
import { allProject } from "@/api/projectManage/project";

export default {
  name: "Approach",
  dicts: ['weighing_type', 'garbage_type'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处置终端车辆进场明细表格数据
      approachList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 过磅类型( 10:进厂;20:出厂 )时间范围
      daterangeCreateData: [],
      // 过磅类型( 10:进厂;20:出厂 )时间范围
      daterangeGrossWeightTime: [],
      // 过磅类型( 10:进厂;20:出厂 )时间范围
      daterangeTareTime: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        disposalLandId: null,
        serialNumber: null,
        carNumber: null,
        shipper: null,
        receivingUnit: null,
        goodsName: null,
        specification: null,
        grossWeightman: null,
        tareWeigher: null,
        grossHeavyweight: null,
        tareHeavy: null,
        createData: null,
        grossWeightTime: null,
        tareTime: null,
        weighingTime: null,
        secondWeighingTime: null,
        garbageWeight: null,
        grossWeight: null,
        tare: null,
        deduction: null,
        netWeight: null,
        unitPrice: null,
        amount: null,
        factor: null,
        squareAmount: null,
        weighingFee: null,
        oneWeighing: null,
        secondWeighing: null,
        grossImg: null,
        tareImg: null,
        garbageType: null,
        weighingType: null,
        projectName: null,
        projectId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 资源利用化企业选项列表
      disposalLandOptions: [],
      projectOptions: []
    };
  },
  created () {
    this.getList();
    this.getDisposalLandList();
    this.getProjectList();
  },
  methods: {
    // 资源利用化企业选项列表
    getDisposalLandList () {
      allDisposalLand().then(response => {
        this.disposalLandOptions = response.data;
      })
    },
    // 项目列表
    getProjectList () {
      allProject().then(response => {
        this.projectOptions = response.data;
      })
    },
    /** 查询处置终端车辆进场明细列表 */
    getList () {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateData && '' != this.daterangeCreateData) {
        this.queryParams.params["beginCreateData"] = this.daterangeCreateData[0];
        this.queryParams.params["endCreateData"] = this.daterangeCreateData[1];
      }
      if (null != this.daterangeGrossWeightTime && '' != this.daterangeGrossWeightTime) {
        this.queryParams.params["beginGrossWeightTime"] = this.daterangeGrossWeightTime[0];
        this.queryParams.params["endGrossWeightTime"] = this.daterangeGrossWeightTime[1];
      }
      if (null != this.daterangeTareTime && '' != this.daterangeTareTime) {
        this.queryParams.params["beginTareTime"] = this.daterangeTareTime[0];
        this.queryParams.params["endTareTime"] = this.daterangeTareTime[1];
      }
      listApproach(this.queryParams).then(response => {
        this.approachList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        disposalLandId: null,
        serialNumber: null,
        carNumber: null,
        shipper: null,
        receivingUnit: null,
        goodsName: null,
        specification: null,
        grossWeightman: null,
        tareWeigher: null,
        grossHeavyweight: null,
        tareHeavy: null,
        createData: null,
        grossWeightTime: null,
        tareTime: null,
        weighingTime: null,
        secondWeighingTime: null,
        garbageWeight: null,
        grossWeight: null,
        tare: null,
        deduction: null,
        netWeight: null,
        unitPrice: null,
        amount: null,
        factor: null,
        squareAmount: null,
        weighingFee: null,
        oneWeighing: null,
        secondWeighing: null,
        grossImg: null,
        tareImg: null,
        garbageType: null,
        weighingType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateData = [];
      this.daterangeGrossWeightTime = [];
      this.daterangeTareTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加处置终端车辆进场明细";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getApproach(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处置终端车辆进场明细";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApproach(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApproach(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delApproach(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('disposal/approach/export', {
        ...this.queryParams
      }, `进出厂记录_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
